<?php
namespace backend\controllers;

use backend\models\SendEmail;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use backend\models\Logs;
use backend\models\Goods;
use backend\models\GoodsClass;
use backend\models\User;
use backend\models\AuthAssignment;
/**
 * Site controller
 */
class SiteController extends Controller
{

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        $this->getView()->title = "供货价管理系统";
        $model = new Goods();

        $query=$model->find()->groupBy('check')->select('check as item_name,count(id) as num');
        $userid=Yii::$app->user->id;


        if(Yii::$app->authManager->checkAccess($userid,'商品列表_供应商') ){

            $rbac=['gys'=>$userid];
        }
        if(Yii::$app->authManager->checkAccess($userid,'商品列表_运营')){

            $rbac=['nextid'=>$userid];
        }

        if(Yii::$app->authManager->checkAccess($userid,'商品列表_下辖一级审批')){


            $user = new User();
            $users = $user->find()->where(['parentid'=>$userid])->select('id')->asArray()->all();
            $u=[];
            foreach($users as $item){
                $u[]=$item['id'];
            }


            $rbac=['in','nextid',$u];


        }
        if(!empty($rbac)){
            $query->andWhere($rbac);
        }
        $user_tongji =     $query->asArray()->all();

//        return $this->redirect("/goods/index");
        $color=['f56954','00a65a','f39c12','00c0ef','3c8dbc','d2d6de'];
        $highlight=['ff0000','00a65a','f39c12','00c0ef','3c8dbc','d2d6de'];
        foreach($user_tongji as $key=>$item){

            if($item['item_name']==0){
                $user_tongji[$key]['item_name']='待审核';
                $user_tongji[$key]['color']=$color[2];
                $user_tongji[$key]['highlight']=$highlight[2];
            }elseif($item['item_name']==1){
                $user_tongji[$key]['item_name']='一审通过';
                $user_tongji[$key]['color']=$color[3];
                $user_tongji[$key]['highlight']=$highlight[3];
            }elseif($item['item_name']==2){
                $user_tongji[$key]['item_name']='二审通过';
                $user_tongji[$key]['color']=$color[4];
                $user_tongji[$key]['highlight']=$highlight[4];
            }elseif($item['item_name']==3){
                $user_tongji[$key]['item_name']='审核通过';
                $user_tongji[$key]['color']=$color[1];
                $user_tongji[$key]['highlight']=$highlight[1];

            }elseif($item['item_name']==4){
                $user_tongji[$key]['item_name']='审核不通过';
                $user_tongji[$key]['color']=$color[0];
                $user_tongji[$key]['highlight']=$highlight[0];

            }

        }
        $model = Goods::find()->where(['>','updatetime',date("Y-m-d 00:00:00")])->andWhere(['<','updatetime',date('Y-m-d 23:59:59')]);
        if(!empty($rbac)){
            $model->andWhere($rbac);
        }
        $total_goods=$model ->count();
        $model = Logs::find()->where(['>','oprtime',date("Y-m-d 00:00:00")])->andWhere(['<','oprtime',date('Y-m-d 23:59:59')])->andWhere(['opid'=>$userid]);

        $total_logs=$model ->count();

//        var_dump($user_tongji);exit;
        return $this->render('index',[

            'total_goods'=>$total_goods,
            'total_goodsclass'=>GoodsClass::find()->count(),
            'total_user'=>User::find()->count(),
            'total_logs'=>$total_logs,
            'user_tongji'=>$user_tongji,
        ]);
    }

    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        $this->getView()->title = "供货价管理系统--登录";
        if (!Yii::$app->user->isGuest) {

            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            $logs = new Logs();
            $logs->opid = Yii::$app->user->id;
            $logs->oprtype='登录';
            $logs->oprtime=date('Y-m-d H:i:s');
            $logs->save();
            return $this->goBack();
        } else {
            $model->password = '';

            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        if(Yii::$app->user->id){
            $logs = new Logs();
            $logs->opid = Yii::$app->user->id;
            $logs->oprtype='退出登录';
            $logs->oprtime=date('Y-m-d H:i:s');
            $logs->save();
        }

        Yii::$app->user->logout();

        return $this->goHome();
    }


}
